package com.yuran.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yuran.common.annotation.Log;
import com.yuran.common.core.controller.BaseController;
import com.yuran.common.core.domain.AjaxResult;
import com.yuran.common.core.page.TableDataInfo;
import com.yuran.common.enums.BusinessType;
import com.yuran.common.utils.poi.ExcelUtil;
import com.yuran.system.domain.SysLabel;
import com.yuran.system.service.ISysLabelService;

/**
 * 标签信息操作处理
 * 
 * @author yuran
 */
@RestController
@RequestMapping("/system/label")
public class SysLabelController extends BaseController
{
    @Autowired
    private ISysLabelService labelService;

    /**
     * 获取标签列表
     */
    @PreAuthorize("@ss.hasPermi('system:label:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysLabel label)
    {
        startPage();
        List<SysLabel> list = labelService.selectLabelList(label);
        return getDataTable(list);
    }
    
    @Log(title = "标签管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:label:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLabel label)
    {
        List<SysLabel> list = labelService.selectLabelList(label);
        ExcelUtil<SysLabel> util = new ExcelUtil<SysLabel>(SysLabel.class);
        util.exportExcel(response, list, "标签数据");
    }

    /**
     * 根据标签编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:label:query')")
    @GetMapping(value = "/{labelId}")
    public AjaxResult getInfo(@PathVariable Long labelId)
    {
        return success(labelService.selectLabelById(labelId));
    }

    /**
     * 新增标签
     */
    @PreAuthorize("@ss.hasPermi('system:label:add')")
    @Log(title = "标签管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysLabel label)
    {
        if (!labelService.checkLabelNameUnique(label))
        {
            return error("新增标签'" + label.getLabelName() + "'失败，标签名称已存在");
        }
        else if (!labelService.checkLabelCodeUnique(label))
        {
            return error("新增标签'" + label.getLabelName() + "'失败，标签编码已存在");
        }
        label.setCreateBy(getUsername());
        return toAjax(labelService.insertLabel(label));
    }

    /**
     * 修改标签
     */
    @PreAuthorize("@ss.hasPermi('system:label:edit')")
    @Log(title = "标签管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysLabel label)
    {
        if (!labelService.checkLabelNameUnique(label))
        {
            return error("修改标签'" + label.getLabelName() + "'失败，标签名称已存在");
        }
        else if (!labelService.checkLabelCodeUnique(label))
        {
            return error("修改标签'" + label.getLabelName() + "'失败，标签编码已存在");
        }
        label.setUpdateBy(getUsername());
        return toAjax(labelService.updateLabel(label));
    }

    /**
     * 删除标签
     */
    @PreAuthorize("@ss.hasPermi('system:label:remove')")
    @Log(title = "标签管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{labelIds}")
    public AjaxResult remove(@PathVariable Long[] labelIds)
    {
        return toAjax(labelService.deleteLabelByIds(labelIds));
    }

    /**
     * 获取标签选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<SysLabel> labels = labelService.selectLabelAll();
        return success(labels);
    }
}
